package com.frxuexi.mine

import com.frxuexi.mine.entity.User
import com.frxuexi.mine.room.AppDataBase
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

class UserRepository private constructor(private val dataBase: AppDataBase) {
    companion object{
        @Volatile
        private var instance: UserRepository?=null

        @JvmStatic
        fun getInstance(dataBase: AppDataBase)= instance ?: synchronized(this){
            instance ?: UserRepository(dataBase = dataBase)
        }
    }

    val userDao=dataBase.userDao()

    suspend fun addUser(user: User):Long{
        return withContext(Dispatchers.IO){
            userDao.addUser(user)
        }
    }

    suspend fun login(name:String,pass:String): User?{
        return withContext(Dispatchers.IO){
            userDao.queryUser(name, pass =pass)
        }
    }
}